<?php

session_start();

ini_set('include_path', '/home/forum/public_html/libs/');
ini_set('error_reporting', E_ALL);  
/**
 *
 * File da includere in ogni pagina che carica automaticamente 
 * tutte le classi sotto la cartella specificata 'php/module/'
 * Inizializza e configura smarty
 * 
 * @author Stefano Poncini
 */
function autoLoader($className) {
    $directories = array(
        'php/module/',
        '../module/',
        '../../module/'
    );


    $fileNameFormats = array(
        '%s.class.php'
    );

    $path = str_ireplace('_', '/', $className);
    if (@include_once $path . '.php') {
        return;
    }
    foreach ($directories as $directory) {
        foreach ($fileNameFormats as $fileNameFormat) {
            $path = $directory . sprintf($fileNameFormat, $className);
            if (file_exists($path)) {
                include_once $path;
                return;
            }
        }
    }
}

spl_autoload_register('autoLoader');

/**
 * 
 * 
 * autoload per la gestione dei namespace ****non funzionante****

  function startsWith($haystack, $needle) {
  $needle = str_replace('\\', '/', $class);
  $length = strlen($needle);
  return (substr($haystack, 0, $length) === $needle);
  }


  function __autoload($class) {
  if(startsWith($class, 'php/module')){
  $class = str_replace('\\', '/', $class) . '.class.php';
  //echo '--------> ' . __DIR__.'/../../'. $class;
  require_once __DIR__.'/../../'. $class;
  }
  }


  use php\module\Form;
  $form = new Form("POST", "#");
  use php\module\User; //
  $user = new User();// vai continua
 * 
 * 
 */

require('php/inc/en.inc.php');

// includo smarty e lo istanzio
include_once("libs/Smarty.class.php");

$smarty = new Smarty();
$user = new User();

// Configurazione generali da includere in tutte le pagine del progetto
$PAGES_URL = 'static/skin/pages/';
$smarty->force_compile = true;
$smarty->debugging = true;
$smarty->caching = true;
$smarty->cache_lifetime = 120;
$smarty->template_dir = $PAGES_URL;
$smarty->compile_dir = 'static/skin/templates_c';
$smarty->loadPlugin('smarty_compiler_switch');

$smarty->assign("TEMPLATES_URL", $PAGES_URL);
$smarty->assign('titolo', 'Forum');
$smarty->assign('lang', $lang);

if (isset($_SESSION['user'])) {
    $user = unserialize($_SESSION['user']);
    $smarty->assign("user", $user);
}

$setting = new Setting();
$smarty->assign("setting", $setting);

if (isset($_GET['action']) && $_GET['action'] == 'logout') {
    $logout = $user->doLogout();

    if ($logout){
        $smarty->assign("logout", $logout);
        KLogger::logToDB($user->username . " ha effettuato il logout", $user->username, KLOGGER::LOGIN);
        
    }
}

// imposto l'ultima visita
if (isset($_SESSION['user'])) {
    $user = unserialize($_SESSION['user']);
    if($user->isLogged()){
        $user->setLastVisit();
    }
}


$post_page = $setting->post_page;
$smarty->assign("post_page", $post_page);

?>